package cn.com.scooper.impl.xf;

import cn.com.scooper.common.exception.BusinessException;
import cn.com.scooper.common.resp.ResultCode;
import cn.com.scooper.dao.xf.MineDeviceAlarmDao;
import cn.com.scooper.pojo.po.MineDeviceAlarmPo;
import cn.com.scooper.pojo.po.MineIotDevicePo;
import cn.com.scooper.pojo.qo.MineDeviceAlarmClearQo;
import cn.com.scooper.pojo.qo.MineDeviceAlarmSaveQo;
import cn.com.scooper.pojo.qo.MineDeviceAlarmUpdateQo;
import cn.com.scooper.service.xf.MineDeviceAlarmService;
import cn.com.scooper.service.xf.MineIotDeviceService;
import cn.com.scooper.utils.MineCityRequestUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Objects;

import static cn.com.scooper.common.constant.CommonMineConstant.*;

@Service
public class MineDeviceAlarmServiceImpl implements MineDeviceAlarmService {
    @Autowired
    private MineDeviceAlarmDao mineDeviceAlarmDao;
    @Autowired
    private MineIotDeviceService mineIotDeviceService;
    @Autowired
    private RestTemplate restTemplate;

    private static final Logger logger = LoggerFactory.getLogger(MineDeviceAlarmServiceImpl.class);

    @Override
    public MineDeviceAlarmPo save(MineDeviceAlarmSaveQo mineDeviceAlarmSaveQo) {
        logger.info("【设备报警新增】数据内容：{}", JSONObject.toJSONString(mineDeviceAlarmSaveQo));
        MineIotDevicePo slopeDevicePo = mineIotDeviceService.getMineIotDeviceByDeviceId(mineDeviceAlarmSaveQo.getDeviceId());
        if (Objects.isNull(slopeDevicePo)){
            throw new BusinessException(ResultCode.DATA_NOEXIST,mineDeviceAlarmSaveQo.getDeviceId() + "该设备信息不存在");
        }

        MineDeviceAlarmPo deviceAlarmQuery = mineDeviceAlarmDao.selectByAlarmId(mineDeviceAlarmSaveQo.getAlarmId());
        if (!Objects.isNull(deviceAlarmQuery)){
            throw new BusinessException(ResultCode.DATA_NOEXIST,mineDeviceAlarmSaveQo.getAlarmId() + "该报警信息已存在");
        }
        MineDeviceAlarmPo mineDeviceAlarmPo = new MineDeviceAlarmPo();
        BeanUtils.copyProperties(mineDeviceAlarmSaveQo,mineDeviceAlarmPo);
        mineDeviceAlarmPo.setEquipId(slopeDevicePo.getSourceId());
        mineDeviceAlarmPo.setStEquipNo(slopeDevicePo.getStSourceId());
        mineDeviceAlarmPo.setDeald(mineDeviceAlarmSaveQo.getDeaId());
        mineDeviceAlarmDao.insert(mineDeviceAlarmPo);

        //保存后进行市级接口上报
        MineCityRequestUtils.mineAndDeviceReport(JSON.toJSONString(mineDeviceAlarmPo),restTemplate,"预警信息新增",CITY_DEVICE_ALARM_ADD_URL);
        return mineDeviceAlarmPo;
    }

    @Override
    public MineDeviceAlarmPo handle(MineDeviceAlarmUpdateQo mineDeviceAlarmUpdateQo) {
        logger.info("【设备报警处置】数据内容：{}", JSONObject.toJSONString(mineDeviceAlarmUpdateQo));
        //编辑前先查询是否存在预警id和设备id
        MineIotDevicePo slopeDevicePo = mineIotDeviceService.getMineIotDeviceByDeviceId(mineDeviceAlarmUpdateQo.getDeviceId());
        if (Objects.isNull(slopeDevicePo)){
            throw new BusinessException(ResultCode.DATA_NOEXIST,"设备信息不存在");
        }

        MineDeviceAlarmPo deviceAlarmQuery = mineDeviceAlarmDao.selectByAlarmId(mineDeviceAlarmUpdateQo.getAlarmId());
        if (Objects.isNull(deviceAlarmQuery)){
            throw new BusinessException(ResultCode.DATA_NOEXIST,"报警信息不存在");
        }

        MineDeviceAlarmPo mineDeviceAlarmPo = new MineDeviceAlarmPo();
        BeanUtils.copyProperties(mineDeviceAlarmUpdateQo,mineDeviceAlarmPo);
        mineDeviceAlarmPo.setEquipId(slopeDevicePo.getSourceId());
        mineDeviceAlarmPo.setStEquipNo(slopeDevicePo.getStSourceId());
        mineDeviceAlarmDao.updateByAlarmId(mineDeviceAlarmPo);
        mineDeviceAlarmPo.setDeald(mineDeviceAlarmUpdateQo.getDeaId());
        //保存后进行市级接口上报
        MineCityRequestUtils.mineAndDeviceReport(JSON.toJSONString(mineDeviceAlarmPo),restTemplate,"预警信息处置",CITY_DEVICE_ALARM_HANDLE_URL);
        return mineDeviceAlarmPo;
    }

    @Override
    public MineDeviceAlarmPo clear(MineDeviceAlarmClearQo mineDeviceAlarmClearQo) {
        logger.info("【设备报警消除】数据内容：{}", JSONObject.toJSONString(mineDeviceAlarmClearQo));
        MineDeviceAlarmPo deviceAlarmQuery = mineDeviceAlarmDao.selectByAlarmId(mineDeviceAlarmClearQo.getAlarmId());
        if (Objects.isNull(deviceAlarmQuery)){
            throw new BusinessException(ResultCode.DATA_NOEXIST,"报警信息不存在");
        }

        MineDeviceAlarmPo mineDeviceAlarmPo = new MineDeviceAlarmPo();
        BeanUtils.copyProperties(mineDeviceAlarmClearQo,mineDeviceAlarmPo);
        mineDeviceAlarmDao.updateByAlarmId(mineDeviceAlarmPo);
        mineDeviceAlarmPo.setEquipId(deviceAlarmQuery.getEquipId());
        mineDeviceAlarmPo.setStEquipNo(deviceAlarmQuery.getStEquipNo());
        //消警后进行市级接口上报
        MineCityRequestUtils.mineAndDeviceReport(JSON.toJSONString(mineDeviceAlarmPo),restTemplate,"预警信息消除",CITY_DEVICE_ALARM_CLEAR_URL);
        return mineDeviceAlarmPo;
    }
}